﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ToolboxData("<{0}:TopMembers runat=server></{0}:TopMembers>")> _
    Public Class TopMembers
        Inherits WebControl
        Private _siteId As Integer = -1
        Private _rows As Integer = 10
        Private _itemTemplate As DisplayTemplate
        Private _headerTemplate As DisplayTemplate
        Private _footerTemplate As DisplayTemplate
        Public Property SiteId() As Integer
            Get
                Return _siteId
            End Get
            Set(ByVal value As Integer)
                _siteId = value
            End Set
        End Property
        Public Property Rows() As Integer
            Get
                Return _rows
            End Get
            Set(ByVal value As Integer)
                _rows = value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
            Public Property ItemTemplate() As DisplayTemplate
            Get
                Return _itemTemplate
            End Get
            Set(ByVal Value As DisplayTemplate)
                _itemTemplate = Value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property HeaderTemplate() As DisplayTemplate
            Get
                Return _headerTemplate
            End Get
            Set(ByVal Value As DisplayTemplate)
                _headerTemplate = Value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property FooterTemplate() As DisplayTemplate
            Get
                Return _footerTemplate
            End Get
            Set(ByVal Value As DisplayTemplate)
                _footerTemplate = Value
            End Set
        End Property
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            Dim sHeaderTemplate As String = String.Empty
            Dim sFooterTemplate As String = String.Empty
            If Not HeaderTemplate Is Nothing Then
                sHeaderTemplate = HeaderTemplate.Text
            End If
            If Not FooterTemplate Is Nothing Then
                sFooterTemplate = FooterTemplate.Text
            End If
            Dim sTemplate As String = "[DISPLAYNAME]"
            Dim db As New Data.Common
            Dim dr As IDataReader = db.TopMembers_Get(SiteId, Rows)
            If Not ItemTemplate Is Nothing Then
                sTemplate = ItemTemplate.Text
            End If
            Dim sb As New Text.StringBuilder
            If String.IsNullOrEmpty(CssClass) Then
                CssClass = "aflist2"
            End If
            While dr.Read
                Dim sOut As String = sTemplate
                sOut = sOut.Replace("[DISPLAYNAME]", dr("DisplayName").ToString)
                sb.Append(sOut)
            End While
            dr.Close()
            dr.Dispose()
            If Not String.IsNullOrEmpty(sb.ToString) Then
                writer.Write(sHeaderTemplate)
                writer.Write(sb.ToString)
                writer.Write(sFooterTemplate)
            End If
        End Sub

    End Class

End Namespace
